package com.hyt.it.ogt.opertaion.track.aspect;

import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.opertaion.track.annotations.KsWarningSave;
import com.hyt.it.ogt.opertaion.track.aspect.base.BaseServiceAspect;
import com.hyt.it.ogt.opertaion.track.model.KsWarningLogVO;
import com.hyt.it.ogt.opertaion.track.queue.OperationTrackBlockQueue;
import com.hyt.it.ogt.opertaion.track.util.BeanUtils;
import com.hyt.it.ogt.opertaion.track.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 朱振宇
 * @since 2022/11/13
 */
@Slf4j
@Aspect
@Order
@Component
@ConditionalOnProperty(value = "ksWarningAspect.enabled", havingValue = "true", matchIfMissing = true)
public class KsWarningAspect extends BaseServiceAspect {

    // 配置织入点
    @Pointcut("execution(* *(..)) && @annotation(ksWarningSave)")
    public void statisticsPointCut(KsWarningSave ksWarningSave) {
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "statisticsPointCut(ksWarningSave)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, KsWarningSave ksWarningSave, Object jsonResult) {
        handleStatistics(joinPoint, ksWarningSave, jsonResult);
    }

    protected void handleStatistics(final JoinPoint joinPoint, KsWarningSave ksWarningSave, Object jsonResult) {
        try {
            boolean push = saveByJoinPoint(joinPoint, ksWarningSave) || saveByJsonResult(jsonResult, joinPoint, ksWarningSave);
            /*if (log.isDebugEnabled()) {
                log.debug("预警日志推送:{}", push);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void execute(List<Map<String, Object>> fieldValMaps, Annotation annotation) {
        List<KsWarningLogVO> ksWarningLogVOList = BeanUtils.convertMapToBeanList(fieldValMaps, KsWarningLogVO.class);
        String createDate = DateUtils.now();
        KsWarningSave ksWarningSave =  (KsWarningSave) annotation;
        ksWarningLogVOList.forEach(vo->{
            if(!StringUtils.isEmpty(ksWarningSave.warnTypeVal())) {
                vo.setWarnType(Integer.valueOf(ksWarningSave.warnTypeVal()));
            }else if(Objects.isNull(vo.getWarnType())) {
                vo.setWarnType(0);
            }
/*            if(!StringUtils.isEmpty(ksWarningSave.isDealValue())) {
                vo.setIsDeal(new Boolean(ksWarningSave.isDealValue()));
            }else if(Objects.isNull(vo.getIsDeal())) {
                vo.setIsDeal(false);
            }*/
            if(!StringUtils.isEmpty(ksWarningSave.createDateValue())) {
                vo.setCreateDate(ksWarningSave.createDateValue());
            }else if(Objects.isNull(vo.getCreateDate())) {
                vo.setCreateDate(createDate);
            }
        });
        OperationTrackBlockQueue.producer(ksWarningLogVOList);
        //OperationTrackExecutorFactory.getThreadPoolExecutor().execute(()-> kwOperationTrackClient.saveWarning(ksWarningLogVOList));
    }
}
